<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :row-style="{ height: '30px' }"  :cell-style="{ padding: '0px' }" border v-loading="loading" :data="registrationList" @selection-change="handleSelectionChange">
<!--      <el-table-column label="企业名称" align="center" prop="activityName" />-->
      <el-table-column label="报名人员姓名" align="center" prop="personnelName" />
      <el-table-column label="报名人员职务" align="center" prop="personnelDuties" />
      <el-table-column label="报名人员联系方式" align="center" prop="personnelContact" />
      <el-table-column label="报名企业" align="center" prop="deptName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            title="编辑"
          ></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            title="删除"
            @click="handleDelete(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动培训报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="人员姓名" prop="personnelName">
          <el-input v-model="form.personnelName" placeholder="请输入报名人员姓名"  maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item label="人员职务" prop="personnelDuties">
          <el-input v-model="form.personnelDuties" placeholder="请输入报名人员职务"  maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item label="联系方式" prop="personnelContact">
          <el-input v-model="form.personnelContact" placeholder="请输入报名人员联系方式"  maxlength="20" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegistration, getRegistration, delRegistration, addRegistration, updateRegistration } from "@/api/activity/registration";

export default {
  name: "receivePerson",
  props:{
    activityId:{
      type: String,
      default: null
    },
    activityName:{
      type:String,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动培训报名信息表格数据
      registrationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        activityId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        personnelName: [
          { required: true, message: '请输入人员姓名', trigger: 'blur' },
          // 可以添加其他规则，例如最大长度、最小长度等
        ],
        personnelDuties: [
          { required: true, message: '请输入人员职务', trigger: 'blur' },
        ],
        personnelContact: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^\d{11}$/, message: '请输入11位数字的联系方式', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询活动培训报名信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.activityId=this.activityId;
      listRegistration(this.queryParams).then(response => {
        this.registrationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        applicationId: null,
        activityId: null,
        personnelId: null,
        personnelName: null,
        personnelDuties: null,
        personnelContact: null,
        deptId: null,
        deptName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applicationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动培训报名信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const applicationId = row.applicationId || this.ids
      getRegistration(applicationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动培训报名信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.activityId=this.activityId;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.applicationId != null) {
            updateRegistration(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegistration(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除姓名为"' + row.personnelName + '"的数据项？').then(function() {
        return delRegistration(row.applicationId,row.activityId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
